/*******************************************************************************
 * Copyright (c) 2012 Daniele Parisi.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Daniele Parisi
 *     neb.inf@gmail.com
 *     http://www.danieleparisi.com/
 ******************************************************************************/
package org.jxreport.html.chart;

import org.jxreport.core.Table;
import org.jxreport.core.views.chart.pie.PieChartView;
import org.jxreport.core.views.chart.pie.Series;
import org.jxreport.html.HTMLView;
import org.w3c.dom.Element;

public class HTMLPieChartView extends PieChartView implements HTMLView {

	public HTMLPieChartView(Element viewElement, Table table) {
		super(viewElement, table);
	}

	@Override
	public String getJQueryCode() {
		String code = this.getInputJSCode() + " ";
		code += "var " + this.id + " = jQuery.jqplot ('" + this.chartId + "', [" + this.id + "Data], " +
			    "{ " + 
			    	"title:'" + super.title + "', " + 
				    "legend: {" + 
				        "show: " + this.isShowLegend() + ", " +
				        "location: 'ne', " +	// compass direction, nw, n, ne, e, se, s, sw, w.
				        "xoffset: 12, " +		// pixel offset of the legend box from the x (or x2) axis.
				        "yoffset: 12, " + 		// pixel offset of the legend box from the y (or y2) axis.
			        "}, " +
				    "seriesDefaults: { " +
			        	"renderer: jQuery.jqplot.PieRenderer, " +
			        	"rendererOptions: { " +
			        		"showDataLabels: true " +
			        	"} " +
				    "}, " +
				    "legend: { show:true, location: 'e' } " +
				 "}); ";
		
		return code;
	}

	@Override
	public String getInnerDivCode() {
		return "<div id=\""+ super.chartId + "\"></div>";
		//return "";
	}

	protected String getInputJSCode() {
		String code = "var " + this.id + "Data = [";
		
		boolean first = true;
		for (Series series: super.getSeries()) {
			if(first) first = false;
			else code += ", ";
			
			code += "['" + series.getTitle() + "', " + series.getValue() + "]";
		}
		
		code += "];";
		return code;
	}
}
